package edu.zqu.collegeField.project.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.framework.openfeign.api.GetWorkerNameApi;
import edu.zqu.collegeField.project.dto.FieldTypeDto;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldType;
import edu.zqu.collegeField.project.mapper.FieldTypeMapper;
import edu.zqu.collegeField.project.service.FieldTypeService;
import edu.zqu.collegeField.project.vo.FieldTypeVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地类型 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldTypeServiceImpl extends ServiceImpl<FieldTypeMapper, FieldType> implements FieldTypeService {
    @Autowired
    private FieldTypeMapper fieldTypeMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;


    @Override
    public List<FieldTypeVo> get(BigInteger collegeId, String name) {//获取场地类型列表
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("name", name);
        List<FieldTypeVo> fieldTypeVoList = BeanMapper.mapList(fieldTypeMapper.get(map), FieldTypeVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldTypeVo fieldTypeVo : fieldTypeVoList) {
            fieldTypeVo.setCollegeName(collegeName);
        }
        return fieldTypeVoList;
    }

    @Override
    public FieldTypeVo getById(BigInteger id) {//获取场地类型
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldTypeVo fieldTypeVo = BeanMapper.map(fieldTypeMapper.getById(map), FieldTypeVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldTypeVo.getCollegeId());
        fieldTypeVo.setCollegeName(collegeName);
        return fieldTypeVo;
    }

    @Override
    public FieldTypeVo getByName(BigInteger collegeId, String name) {//获取场地类型
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("name", StrUtil.trimToNull(name));
        FieldTypeVo fieldTypeVo = BeanMapper.map(fieldTypeMapper.getByName(map), FieldTypeVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        fieldTypeVo.setCollegeName(collegeName);
        return fieldTypeVo;
    }

    @Override
    public String getNameById(BigInteger id) {//获取场地类型名称
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        return fieldTypeMapper.getNameById(map);
    }

    @Override
    public int add(FieldType fieldType) {//添加场地类型
        fieldType.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldTypeMapper.insert(fieldType);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldType fieldTypeUpdate) {
        FieldType fieldType = fieldTypeMapper.selectById(fieldTypeUpdate.getId());
        if (StringUtils.isNull(fieldType)) {
            throw new NullPointerException("找不到该场地类型");
        }
        BeanMapper.copyPropertiesNotNull(fieldTypeUpdate, fieldType);
        try {
            int insert = fieldTypeMapper.insert(fieldType.setLastVersion(BigInteger.valueOf(fieldTypeUpdate.getId())));
            if (insert >= 1) {
                return delete(fieldTypeUpdate.getId(), fieldTypeUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(Integer id, BigInteger byWho) {
        try {
            UpdateWrapper<FieldType> fieldTypeUpdateWrapper = new UpdateWrapper<FieldType>().eq("id", Convert.toStr(id));
            int update = fieldTypeMapper.update(new FieldType().setByWho(byWho), fieldTypeUpdateWrapper);
            if (update >= 1) {
                return fieldTypeMapper.delete(fieldTypeUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
